import { Controller } from "../../mvc/core/Controller";
import { ICountryMoneyController } from "./interfaces/ICountryMoneyController";
import { ETaskType } from "../../../Message/Game/ETaskType";
import { Alert } from "../../../com/mui/controls/Alert";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { CountryMoneyCanOpenHintBtn } from "../view/countryMoney/CountryMoneyCanOpenHintBtn";
import { CountryMoneyOpenedHintBtn } from "../view/countryMoney/CountryMoneyOpenedHintBtn";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { IView } from "../../mvc/interfaces/IView";
import { CountryMoneyModule } from "../../../modules/CountryMoneyModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SCountryMoneyCopyRank } from "../../../Message/Public/SCountryMoneyCopyRank";
import { ItemData } from "../resource/info/ItemData";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { Language } from "../../../extend/language/Language";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { SCountryMoneyUpdate } from "../../../Message/Game/SCountryMoneyUpdate";
import { SCountryMoneyRecord } from "../../../Message/Game/SCountryMoneyRecord";
import { SActiveOpen } from "../../../Message/Public/SActiveOpen";
import { ICountryMoneyModule } from "../../../modules/interfaces/ICountryMoneyModule";
import { SCountryMoneyActiveInfo } from "../../../Message/Public/SCountryMoneyActiveInfo";
import { ECountryMoneyActiveStatus } from "../../../Message/Public/ECountryMoneyActiveStatus";
import { ImagesConst } from "../resource/ImagesConst";
import { CountryPreciousPanel } from "../view/countryMoney/CountryPreciousPanel";
import { MsgManager } from "../manager/MsgManager";
import { CountryMoneyRankWin } from "../view/countryMoney/CountryMoneyRankWin";
import { HintIcon } from "../view/uiIconBtn/HintIcon";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
type int = number;
//class CountryMoneyController
    
    export  class CountryMoneyController extends Controller implements ICountryMoneyController
    {
       

        /* internal  */onCloseResult(arg1: int): void
        {
            var loc2=null;
            var loc1=cache.task.getTaskByType(ETaskType._ETaskTypeCountryMoneyKnow)[0];
            if (loc1 != null) 
            {
                loc2 = loc1.getTargetByIndex(0);
            }
            if (loc2 == null) 
            {
                loc2 = cache.scene.getNpcByNpcId(1030073);
                if (loc2 == null) 
                {
                    return;
                }
            }
            if (arg1 != Alert.OK) 
            {
                if (arg1 == Alert.CANCEL) 
                {
                    AIManager.onAIControl(loc2, AIType.AI_Convey);
                }
            }
            else 
            {
                AIManager.onAutoPathAIControl(loc2);
            }
            return;
        }

        /* internal  */showCanOPenBtn(): void
        {
            this.hideAllHintBtns();
            if (this._btnCanOpen == null) 
            {
                this._btnCanOpen = new CountryMoneyCanOpenHintBtn();
            }
            this._btnCanOpen.showEffect(true);
            this._btnCanOpen.show();
            return;
        }

        /* internal  */showOpened(): void
        {
            this.hideAllHintBtns();
            if (this._btnOpened == null) 
            {
                this._btnOpened = new CountryMoneyOpenedHintBtn();
            }
            this._btnOpened.showEffect(true);
            this._btnOpened.show();
            return;
        }

        /* internal  */hideAllHintBtns(): void
        {
            if (this._btnCanOpen != null) 
            {
                this._btnCanOpen.hide();
                this._btnCanOpen.dispose();
                this._btnCanOpen = null;
            }
            if (this._btnOpened != null) 
            {
                this._btnOpened.hide();
                this._btnOpened.dispose();
                this._btnOpened = null;
            }
            return;
        }

        /* internal  */onCountryGiveCountryMoneyHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Map<any, any> /* flash.utils.Dictionary */;
            GameProxy.country.giveCountryMoney(loc1);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._countryMoneyModule == null) 
            {
                this._countryMoneyModule = new CountryMoneyModule();
            }
            return this._countryMoneyModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.CountryMoneyWindowOpen, this.onCountryMoneyWindowOpenHandler);
            Dispatcher.addEventListener(EventName.CountryMoneyWindowHide, this.hideModuleHadandler);
            Dispatcher.addEventListener(EventName.CountryGiveCountryMoney, this.onCountryGiveCountryMoneyHandler);
            Dispatcher.addEventListener(EventName.CountryGetMoneyRecord, this.onCountryGetMoneyRecordHandler);
            Dispatcher.addEventListener(EventName.CountryGetMoneyRecordSuccess, this.onCountryGetMoneyRecordSuccessHandler);
            Dispatcher.addEventListener(EventName.CountryMoneyPropPackUse, this.onCountryMoneyPropPackUseHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryMoneyUpdate, this.onCountryMoneyUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryMoneyRecord, this.onCountryMoneyRecordHandler);
            NetDispatcher.addCmdListener(ServerCommand.CountryContributionUpdate, this.onCountryContributionUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryMoneyPrepare, this.countryMoneyPrepearHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryMoneyOpen, this.countryMoneyOpenHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryMoneyClose, this.countryMoneyCloseHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryMoneyActiveInfo, this.activeInfoHandler);
            Dispatcher.addEventListener(EventName.CountryMoneyChangeToTab, this.changeToTabHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            Dispatcher.addEventListener(EventName.CountryMoneyOpenReq, this.openCountryPreciousReqHandler);
            Dispatcher.addEventListener(EventName.CountryMoneyEnterReq, this.enterCountryPreciousReqHandler);
            Dispatcher.addEventListener(EventName.CountryMoneyShowLastRank, this.showLastPresiousRankHandler);
            Dispatcher.addEventListener(EventName.CountryMoneyShowPreciousHelp, this.showPresiousHelpHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.taskGetHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.taskUpdateHandler);
            Dispatcher.addEventListener(EventName.CountryMoneyShowCopyEntranceReq, this.showCountryPreciousEntranceHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryMoneyCopyRank, this.rankResultHandler);
            return;
        }

        /* internal  */onCountryMoneyWindowOpenHandler(arg1: DataEvent): void
        {
            view.show();
            return;
        }

        /* internal  */hideModuleHadandler(arg1: DataEvent): void
        {
            if (!(_view == null) && !_view.isHide) 
            {
                _view.hide();
            }
            return;
        }

        /* internal  */rankResultHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCountryMoneyCopyRank;
            if (loc1 == null) 
            {
                return;
            }
            if (this._rankWin == null) 
            {
                return;
            }
            this._rankWin.updateData(loc1);
            return;
        }

        /* internal  */onCountryGetMoneyRecordHandler(arg1: DataEvent): void
        {
            GameProxy.country.getCountryMoneyRecords();
            return;
        }

        /* internal  */onCountryGetMoneyRecordSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.records as Array<any>;
            var loc3=loc1.money as int;
            cache.country.countryMoneyRecords = loc2;
            cache.role.countryMoney = loc3;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryMoneyUpdate));
            if (this._countryMoneyModule && !this._countryMoneyModule.isHide) 
            {
                this._countryMoneyModule.updateCountryMoneyRecords();
                this._countryMoneyModule.updateCountryMoney(loc3);
            }
            return;
        }

        /* internal  */onCountryMoneyPropPackUseHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            var loc2="";
            if (ItemsUtil.isCountryStaticProp(loc1)) 
            {
                loc2 = Language.getStringByParam(36500, loc1.effectEx, loc1.effect);
            }
            else if (ItemsUtil.isCountryDynamicProp(loc1) && loc1.itemExInfo) 
            {
                loc2 = Language.getStringByParam(36500, loc1.itemExInfo.it_c_mon, loc1.itemExInfo.it_c_con);
            }
            if (loc2 != "") 
            {
                Alert.okLabel = Language.getString(36501);
                Alert.calcelLabel = Language.getString(36502);
                Alert.buttonWidth = 80;
                Alert.showCloseBtn = true;
                Alert.show(loc2, null, Alert.OK | Alert.CANCEL, null, this.onPropUseCloseHandler);
            }
            return;
        }

        /* internal  */removeRankWinHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._rankWin == null) 
            {
                return;
            }
            this._rankWin.removeEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.removeRankWinHandler);
            this._rankWin.dispose(true);
            this._rankWin = null;
            return;
        }

        /* internal  */onPropUseCloseHandler(arg1: int): void
        {
            if (this._targetInfo == null) 
            {
                this._targetInfo = cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectCountryMoney, 400101);
            }
            if (arg1 != Alert.OK) 
            {
                if (arg1 == Alert.CANCEL) 
                {
                    AIManager.onAIControl(this._targetInfo, AIType.AI_Convey);
                }
            }
            else 
            {
                AIManager.onAutoPathAIControl(this._targetInfo);
            }
            return;
        }

        /* internal  */onCountryMoneyUpdateHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCountryMoneyUpdate;
            if (loc1 == null) 
            {
                return;
            }
            cache.role.countryMoney = loc1.updateValue;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryMoneyUpdate));
            if (this._countryMoneyModule && !this._countryMoneyModule.isHide) 
            {
                this._countryMoneyModule.updateCountryMoney(loc1.updateValue);
            }
            return;
        }

        /* internal  */onCountryMoneyRecordHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCountryMoneyRecord;
            cache.country.addNewRecord(loc1);
            if (this._countryMoneyModule && !this._countryMoneyModule.isHide) 
            {
                this._countryMoneyModule.updateCountryMoneyRecords();
            }
            return;
        }

        /* internal  */onCountryContributionUpdateHandler(arg1: int): void
        {
            if (this._countryMoneyModule && !this._countryMoneyModule.isHide) 
            {
                this._countryMoneyModule.updateContribution(arg1);
            }
            return;
        }

        /* internal  */countryMoneyPrepearHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SActiveOpen;
            cache.countryMoney.prePearInfo = loc1;
            this.showCanOPenBtn();
            return;
        }

        /* internal  */countryMoneyOpenHandler(arg1: MessageBlock): void
        {
            cache.countryMoney.isOpening = true;
            cache.countryMoney.isClose = false;
            this.showOpened();
            GameProxy.copy.getCountryMoneyCopyInfo();
            return;
        }

        /* internal  */countryMoneyCloseHandler(arg1: MessageBlock): void
        {
            this.hideAllHintBtns();
            cache.countryMoney.isClose = true;
            cache.countryMoney.isOpening = false;
            return;
        }

        /* internal  */changeToTabHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            if (loc1 == CountryMoneyController.OpenCountryMoneyTabIndex) 
            {
                GameProxy.copy.getCountryMoneyCopyInfo();
            }
            return;
        }

        /* internal  */showCountryPreciousEntranceHandler(arg1: DataEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryMoneyWindowOpen));
            (view as ICountryMoneyModule).changeToTab(CountryMoneyController.OpenCountryMoneyTabIndex);
            return;
        }

        /* internal  */activeInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCountryMoneyActiveInfo;
            cache.countryMoney.activeInfo = loc1;
            this.updateAllOpenCountryMoneyInfo(loc1);
            return;
        }

        /* internal  */updateAllOpenCountryMoneyInfo(arg1: SCountryMoneyActiveInfo): void
        {
            var loc1=arg1.status;
            switch (loc1) 
            {
                case ECountryMoneyActiveStatus._ECountryMoneyActiveStatusClose:
                {
                    (view as ICountryMoneyModule).setEnterBtnLabel(ImagesConst.CountryMoneyOpenPrecious, false);
                    (view as ICountryMoneyModule).setCountryMoneyOpenMode(CountryPreciousPanel.OpenMode_CountDown);
                    (view as ICountryMoneyModule).updateNextOpenDate(cache.countryMoney.nextOpenTimeSeconds);
                    break;
                }
                case ECountryMoneyActiveStatus._ECountryMoneyActiveStatusPrepare:
                {
                    (view as ICountryMoneyModule).setCountryMoneyOpenMode(CountryPreciousPanel.OpenMode_CanOpen);
                    (view as ICountryMoneyModule).setEnterBtnLabel(ImagesConst.CountryMoneyOpenPrecious, cache.country.doIHavePrivilege());
                    break;
                }
                case ECountryMoneyActiveStatus._ECountryMoneyActiveStatusOpen:
                {
                    (view as ICountryMoneyModule).setCountryMoneyOpenMode(CountryPreciousPanel.OpenMode_Opening);
                    (view as ICountryMoneyModule).setEnterBtnLabel(ImagesConst.CountryMoneyEnter, true);
                    break;
                }
                case ECountryMoneyActiveStatus._ECountryMoneyActiveStatusTest:
                {
                    (view as ICountryMoneyModule).setCountryMoneyOpenMode(CountryPreciousPanel.OpenMode_Test);
                    (view as ICountryMoneyModule).setEnterBtnLabel(ImagesConst.CountryMoneyOpenPrecious, false);
                    break;
                }
            }
            (view as ICountryMoneyModule).updateCurrentCountryGold(cache.countryMoney.totalCountryMoney);
            (view as ICountryMoneyModule).updateProduceBox(cache.countryMoney.produceBox);
            (view as ICountryMoneyModule).updateProduceYB(cache.countryMoney.produceYB);
            (view as ICountryMoneyModule).updateProduceCoin(cache.countryMoney.produceTB);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1: Object): void
        {
            if (this._countryMoneyModule && !this._countryMoneyModule.isHide) 
            {
                this._countryMoneyModule.updateCountryProp();
            }
            return;
        }

        /* internal  */openCountryPreciousReqHandler(arg1: DataEvent): void
        {
            if (cache.countryMoney.isOpening) 
            {
                return;
            }
            if (!cache.countryMoney.isCanOpen) 
            {
                return;
            }
            if (!cache.country.doIHavePrivilege()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80433));
                return;
            }
            GameProxy.copy.openCountryMoneyCopy();
            return;
        }

        /* internal  */enterCountryPreciousReqHandler(arg1: DataEvent): void
        {
            if (cache.role.roleInfo.level < 60) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80453));
            }
            if (!cache.countryMoney.isOpening) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80434));
                return;
            }
            GameProxy.copy.enterCountryMoneyCopy();
            return;
        }

        /* internal  */showLastPresiousRankHandler(arg1: DataEvent): void
        {
            if (this._rankWin == null) 
            {
                this._rankWin = new CountryMoneyRankWin();
                this._rankWin.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.removeRankWinHandler);
            }
            if (this._rankWin.isHide) 
            {
                this._rankWin.show();
            }
            else 
            {
                this._rankWin.hide();
            }
            return;
        }

        /* internal  */showPresiousHelpHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */taskUpdateHandler(arg1: Object=null): void
        {
            if (cache.task.hasTaskByType(ETaskType._ETaskTypeCountryMoneyKnow) || cache.task.hasCanGetTaskByType(ETaskType._ETaskTypeCountryMoneyKnow)) 
            {
                this.showHintIcon();
            }
            else 
            {
                this.hideHitIcon();
            }
            return;
        }

        /* internal  */taskGetHandler(arg1: Object): void
        {
            var loc1=arg1.task;
            if (loc1 == null) 
            {
                return;
            }
            if (loc1.sTask.code != 2001023) 
            {
                if (loc1.sTask.code == 2001024) 
                {
                    this.hideHitIcon();
                    this.onCountryMoneyWindowOpenHandler(null);
                }
            }
            else 
            {
                this.showHintIcon();
            }
            return;
        }

        /* internal  */showHintIcon(): void
        {
            if (this._hintIcon == null) 
            {
                this._hintIcon = new HintIcon(ImagesConst.CountryMoney_PiaoZi, false);
                this._hintIcon.addEventListener(flash.events.MouseEvent.CLICK, this.onHintIconClickHandler);
            }
            if (this._hintIcon.parent == null) 
            {
                UiHintIconView.instance.addHintIcon(this._hintIcon, true);
            }
            return;
        }

        /* internal  */hideHitIcon(): void
        {
            if (this._hintIcon != null) 
            {
                UiHintIconView.instance.removeHintIcon(this._hintIcon);
            }
            return;
        }

        /* internal  */onHintIconClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=Language.getString(80438);
            Alert.okLabel = Language.getString(80439);
            Alert.buttonWidth = 60;
            Alert.calcelLabel = Language.getString(80440);
            Alert.showCloseBtn = true;
            Alert.show(loc1, null, Alert.OK | Alert.CANCEL, null, this.onCloseResult);
            return;
        }

        public static /* const */OpenCountryMoneyTabIndex: int=1;

        private /* var */_countryMoneyModule: ICountryMoneyModule;

        private /* var */_hintIcon: HintIcon;

        private /* var */_btnCanOpen: CountryMoneyCanOpenHintBtn;

        private /* var */_btnOpened: CountryMoneyOpenedHintBtn;

        private /* var */_rankWin: CountryMoneyRankWin;

        private /* var */_targetInfo: TaskTargetInfo;
    }
